package com.example.livesettingtest;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import floatWindow2.FloatActionController;
import floatWindow2.FloatPermissionManager;
import floatWindow2.rom.HuaweiUtils;
import floatWindow2.rom.MiuiUtils;
import floatWindow2.rom.OppoUtils;
import floatWindow2.rom.RomUtils;


public class KeepLivingSettingsForOPPO extends AppCompatActivity implements View.OnClickListener {

    String TAG = "KeepLivingSettings";
    RelativeLayout permissionSettingsLayout;
    RelativeLayout autoStartLayout;
    RelativeLayout notifySettingsLayout;
    RelativeLayout batteryOptionSettingsLayout;
    RelativeLayout wlanConnectSetting;
    RelativeLayout multitaskSetting;
    Button btTest;
    TextView tvHelpKeepLive;
    int mBrandId; //品牌ID
    boolean mIsPermissionFloat = false;
    //gxw-小米不需要boolean isBelowAndroid8 = false;
    int mOperation = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_keep_living_settings_for_oppo);
        super.onCreate(savedInstanceState);
        getLastIntentData();
        initView();
        String brand = (android.os.Build.BRAND);  //品牌系列，如华为公司的 huawei,honor
        String lowerLetterBrand = brand.toLowerCase();  //品牌
        Log.i(TAG, "brand="+lowerLetterBrand);
        //gxw-  Toast.makeText(this, lowerLetterBrand, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onResume() {
        Log.i(TAG,"Service MainActivity onResume mOperation="+mOperation);
        FloatActionController.getInstance().stopMonkServer(this);
        //gxw:其实这里isPermission返回false时并不代表用户没有打开权限原因未知，返回true那绝对是打开了权限。
        mIsPermissionFloat = FloatPermissionManager.getInstance().applyFloatWindow(this);

        if(mOperation == 0 || mOperation == 3)
        {
            startService();
        }

        super.onResume();
    }

    public void startService()
    {
        ;
    }



    public void getLastIntentData()
    {
        Intent i = this.getIntent();
        Bundle b = i.getExtras();

        if(b != null) {
            mBrandId = b.getInt("brandId",-1);
        }
    }

    private void initView() {
        btTest = this.findViewById(R.id.btTest);
        permissionSettingsLayout = this.findViewById(R.id.permissionSettingsLayout);
        notifySettingsLayout = this.findViewById(R.id.notifySettingsLayout);
        batteryOptionSettingsLayout = this.findViewById(R.id.batteryOptionSettingsLayout);
        wlanConnectSetting = this.findViewById(R.id.wlanConnectSetting);
        multitaskSetting = this.findViewById(R.id.multitaskSetting);
        tvHelpKeepLive = this.findViewById(R.id.tvHelpKeepLive);
        autoStartLayout = this.findViewById(R.id.autoStartLayout);

        permissionSettingsLayout.setOnClickListener(this);
        notifySettingsLayout.setOnClickListener(this);
        batteryOptionSettingsLayout.setOnClickListener(this);
        wlanConnectSetting.setOnClickListener(this);
        multitaskSetting.setOnClickListener(this);
        tvHelpKeepLive.setOnClickListener(this);
        autoStartLayout.setOnClickListener(this);
        btTest.setOnClickListener(this);

        //判断系统版本是否为7.0及以下.
     /*小米不需要   if(Build.VERSION.SDK_INT < 26)
        {
            lockClearSettingsLayout.setVisibility(View.VISIBLE);
            isBelowAndroid8 = true;
        }*/
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {

            case R.id.autoStartLayout:  //自启动和关联启动
                mOperation = 1;
                OppoUtils.getInstance(this).autoStart(mIsPermissionFloat);
                break;

            case R.id.permissionSettingsLayout:  //权限
                mOperation = 0;
                OppoUtils.getInstance(this).openPermission(MyConstant.tipType.OPPO_OPEN_PERMISSION,mIsPermissionFloat);
                break;

            case R.id.batteryOptionSettingsLayout:  //耗电保护
                mOperation = 2;
                OppoUtils.getInstance(this).batteryProtected(mIsPermissionFloat);
                break;
            case R.id.notifySettingsLayout: //允许通知
                mOperation = 3;
                OppoUtils.getInstance(this).openPermission(MyConstant.tipType.OPPO_PERMISSION_NOTIFY,mIsPermissionFloat);
                break;
            case R.id.btTest:
                test();
                break;
        }
    }


    /**
     * 引导用户
     */
    public void test() {


       String mtype = android.os.Build.MODEL; //型号
  /*    java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerConsumptionActivity  com.coloros.oppoguardelf;
        com.coloros.powermanager.fuelgaue.PowerConsumptionActivity;*/

        //耗电保护
/*      try
        {
            Toast.makeText(KeepLivingSettingsForOPPO.this, mtype, Toast.LENGTH_LONG).show();
            Intent intent = new Intent();
            ComponentName componentName = null;
            String packName = this.getPackageName();
           //gxw- componentName = new ComponentName("com.coloros.safecenter", "appfrozen.activity.AppFrozenSettingsActivity");
            componentName = new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerConsumptionActivity");
         //gxw-   componentName = new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerUsageModelActivity");
           //gxw- componentName = new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerAppsBgSetting");
           //gxw- componentName = ComponentName.unflattenFromString("com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerUsageModelActivity");
            intent.putExtra("package_label", this.getString(R.string.app_name));



            int appUid = this.getApplicationInfo().uid;
            intent.putExtra("app_package", packName);
            intent.putExtra("app_uid", appUid);
            // for Android O
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, packName);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(componentName);

            this.startActivity(intent);
        }catch (Exception e)
        {
            e.printStackTrace();
        }*/


        //耗电保护
        try
        {
            Intent intent = new Intent();
            ComponentName componentName = null;
            String packName = this.getPackageName();
            componentName = new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerConsumptionActivity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(componentName);
            this.startActivity(intent);
            MiuiUtils.openFloatWindowTip(MyConstant.tipType.OPPO_BATTERY_PROTECT,true);
        }catch (Exception e)
        {
           Toast.makeText(this, "跳转异常", Toast.LENGTH_SHORT).show();
        }
    }



    /**
     * 获取小米 rom 版本号，获取失败返回 -1
     *
     * @return miui rom version code, if fail , return -1
     */
    public String getMiuiVersion() {
        String version = RomUtils.getSystemProperty("ro.miui.ui.version.name");
        if (version != null) {
            try {
                return version;
            } catch (Exception e) {
                Log.e(TAG, "get miui version code error, version : " + version);
                Log.e(TAG, Log.getStackTraceString(e));
            }
        }
        return version;
    }

    public void gotoKeepLiveHelpDoc()
    {
        Log.i(TAG,"brandId = "+mBrandId);
        String live_setting = this.getResources().getString(R.string.live_setting);
        String appLiveUrl = CommonUtil.getAppLiveSettingUrl(mBrandId);

        Intent liveSettings = new Intent(KeepLivingSettingsForOPPO.this,WebViewBrowser.class);
        liveSettings.putExtra("url",appLiveUrl);
        liveSettings.putExtra("title",live_setting);
        this.startActivity(liveSettings);
    }
}
